package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.DelayCorrectionReq;
import com.joysuch.wwyt.bp.bean.SaveRewardReq;
import com.joysuch.wwyt.bp.entity.BpInspection;
import com.joysuch.wwyt.bp.entity.vo.BpInspectionVo;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.service.BaseConfigService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;


import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.joysuch.wwyt.bp.service.IBpInspectionService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


/**
 *  控制器
 *
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("bp/bpinspection")
@Api(value = "", tags = "接口")
public class BpInspectionController {

	private BaseConfigService baseConfigService;
	private IBpInspectionService bpInspectionService;
	private static final Logger logger = LoggerFactory.getLogger(BpInspectionController.class);

	@PostMapping("save")
	public ResultBean saveOrUpdate(@RequestBody BpInspectionVo bpInspectionVo){
		try{
			BpInspection bpInspection = bpInspectionService.saveOrUpdate(bpInspectionVo);
			return  ResultBean.success(bpInspection);
		}catch (Exception e) {
			logger.error("保存失败", e);
			return ResultBean.fail(101, "保存失败");
		}
	}

	@GetMapping("listPage")
	public ResultBean listPage(Integer page,Integer size){
		List<BpInspection> list =  bpInspectionService.listPage(page,size);
		return  ResultBean.success(list);
	}

	@GetMapping("list")
	public ResultBean listPageCondition(@RequestParam Map<String,String> params){
		try{
			List<BpInspectionVo> list =  bpInspectionService.listPageCondition(params);
			return ResultBean.success(list);
		}catch (Exception e){
			logger.error("查询列表失败", e);
			return ResultBean.fail(101, "查询列表失败");
		}

	}

	@GetMapping("detail")
	public ResultBean detail(@RequestParam("id") Long id){
		try{
			BpInspectionVo bpInspectionVo = bpInspectionService.getInspectionById(id);
			return  ResultBean.success(bpInspectionVo);
		}catch (Exception e){
			logger.error("查询详情失败", e);
			return ResultBean.fail(101, "查询详情失败");
		}

	}

	@GetMapping("delete")
	public ResultBean delete(@RequestParam("id") Long id){
		try{
			bpInspectionService.deleteInspectionById(id);
			return  ResultBean.success("");
		}catch (Exception e){
			logger.error("删除失败", e);
			return ResultBean.fail(101, "删除失败");
		}

	}

	@GetMapping("/get-reward")
	public ResultBean getReward() {
		BaseConfig config = baseConfigService.findConfig("datareport.quanyuanducha.reward");
		if (config == null) {
			return ResultBean.success(0);
		}
		return ResultBean.success(config.getValue());
	}

	@PostMapping("/save-reward")
	public ResultBean saveReward(@RequestBody SaveRewardReq req) {
		BaseConfig config = baseConfigService.findConfig("datareport.quanyuanducha.reward");
		if (config == null) {
			baseConfigService.addConfig("datareport.quanyuanducha.reward", req.getReward() + "");
		} else {
			config.setValue(req.getReward() + "");
			baseConfigService.updateConfig(config);
		}
		return ResultBean.defaultSuccessResult();
	}


	@GetMapping("/export")
	public ResultBean export(@RequestParam Map<String,String> params, HttpServletResponse response) {

		try {
			HSSFWorkbook wb = bpInspectionService.export(params);
			ServletOutputStream outputStream = response.getOutputStream();
			String name = "全员督查.xls";
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name, "UTF-8"));
			wb.write(outputStream);
			outputStream.close();
			return ResultBean.defaultSuccessResult();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return ResultBean.fail(101, "下载失败");
		}
	}

	@PostMapping("/delay-correction")
	public ResultBean delayCorrection(@RequestBody DelayCorrectionReq req) {
		bpInspectionService.delayCorrection(req);

		return ResultBean.defaultSuccessResult();
	}
}
